package com.tianma315.wx.service;


import com.tianma315.core.sys.domain.RoleDO;
import com.tianma315.core.sys.domain.UserDO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

/**
 * 用户业务
 * Created by zcm on 2018/5/14.
 */
public interface UserServices extends UserDetailsService {

    /**
     * 自定义UserDetails
     */
    class DefaultUserDetails implements UserDetails {

        private UserDO user;
        private List<RoleDO> roles;

        public DefaultUserDetails(UserDO user, List<RoleDO> roles) {
            this.user = user;
            this.roles = roles;
        }

        public UserDO getUser() {
            return user;
        }


        public List<RoleDO> getRoles() {
            return roles;
        }


        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            if (roles == null || roles.isEmpty())
                return grantedAuthorities;

            for (RoleDO role : roles) {
                grantedAuthorities.add(() -> {
                    return role.getRoleName();
                });
            }
            return grantedAuthorities;
        }

        @Override
        public String getPassword() {
            return user.getPassword();
        }

        @Override
        public String getUsername() {
            return user.getUsername();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    }


    UserDO getUserDOByUserName(String userName);
}
